﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class TextBoxControl : UserControl, IControlRulable
    {
        private CommandUIDefinitionClasses.TextBox innerTextBox;

        public CommandUIDefinitionClasses.TextBox InnerTextBox
        {
            get { return innerTextBox; }
            set { innerTextBox = value; }
        }

        public TextBoxControl()
        {
            InitializeComponent();
            this.innerTextBox = new CommandUIDefinitionClasses.TextBox();
            this.FillControls();
        }

        public TextBoxControl(CommandUIDefinitionClasses.TextBox textBox):this()
        {
            this.innerTextBox = textBox;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcCommand.Value = this.innerTextBox.Command;
                this.strId.Value = this.innerTextBox.Id;
                this.bcImeEnabled.Value = BoolHelper.GetBoolValue(this.innerTextBox.ImeEnabled);
                this.icMaxLength.Value = this.innerTextBox.MaxLength;
                this.strQueryCommand.Value = this.innerTextBox.QueryCommand;
                this.icSequence.Value = this.innerTextBox.Sequence;
                this.strTemplateAlias.Value = this.innerTextBox.TemplateAlias;
                this.rcToolTipDescription.Value = this.innerTextBox.ToolTipDescription;
                this.strToolTipKeyWord.Value = this.innerTextBox.ToolTipHelpKeyWord;
                this.rcToolTipImage32by32.Value = this.innerTextBox.ToolTipImage32by32;
                this.rcToolTipImage32by32Class.Value = this.innerTextBox.ToolTipImage32by32Class;
                this.icToolTipImage32by32Left.Value = this.innerTextBox.ToolTipImage32by32Left;
                this.icToolTipImage32by32Top.Value = this.innerTextBox.ToolTipImage32by32Top;
                this.strToolTipShortcutKey.Value = this.innerTextBox.ToolTipShortcutKey;
                this.rcToolTipTitle.Value = this.innerTextBox.ToolTipTitle;
                this.strWidth.Value = this.innerTextBox.Width;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerTextBox.Command = this.rcCommand.Value;
                    this.innerTextBox.Id = this.strId.Value;
                    this.innerTextBox.ImeEnabled = this.bcImeEnabled.Value;
                    this.innerTextBox.MaxLength = this.icMaxLength.Value;
                    this.innerTextBox.QueryCommand = this.strQueryCommand.Value;
                    this.innerTextBox.Sequence = this.icSequence.Value;
                    this.innerTextBox.TemplateAlias = this.strTemplateAlias.Value;
                    this.innerTextBox.ToolTipDescription = this.rcToolTipDescription.Value;
                    this.innerTextBox.ToolTipHelpKeyWord = this.strToolTipKeyWord.Value;
                    this.innerTextBox.ToolTipImage32by32 = this.rcToolTipImage32by32.Value;
                    this.innerTextBox.ToolTipImage32by32Class = this.rcToolTipImage32by32Class.Value;
                    this.innerTextBox.ToolTipImage32by32Left = this.icToolTipImage32by32Left.Value;
                    this.innerTextBox.ToolTipImage32by32Top = this.icToolTipImage32by32Top.Value;
                    this.innerTextBox.ToolTipShortcutKey = this.strToolTipShortcutKey.Value;
                    this.innerTextBox.ToolTipTitle = this.rcToolTipTitle.Value;
                    this.innerTextBox.Width = this.strWidth.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void TextBoxControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerTextBox.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
